const app = getApp()
import {
  HomeIndex
} from 'index-modal.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new HomeIndex(); //实例化 首页 对象


Page({
  data: {
    
    windowWidth:0,
    phone:'',
    cose:'',
    time:60,
    ishi:true
    
  },
  onLoad: function(options) {
    this.setData({
      windowWidth: app.globalData.windowWidth,
      
    })
  },

  getphone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  getmima(e){
    this.setData({
      cose: e.detail.value
    })
  },

  _common(){  //发送验证码
    let that = this;
    let data ={
      phone: that.data.phone
    }
    index.common(data,(res)=>{
      if(res.code == 200){
        wx.showToast({
          title:res.msg,
          duration:1000,
          icon:'none'
        })
      } else {
        wx.showToast({title: res.msg,duration: 1000,icon: 'none'})
      }
    })
  },
  _perfect(){  //绑定手机
    let that = this;
    wx.setStorageSync('phoneNumber', that.data.phone);
    let data = {
      phone: that.data.phone,
      code:that.data.cose,
      uid: wx.getStorageSync('members').id,
      type:1
    }
    let token = wx.getStorageSync('members').token || '';
    if(token!=''){
      index.perfect(data,(res)=>{
         if(res.code == 200){
           wx.showToast({
             title: res.msg,
             duration:1000,
             icon: 'none'
           })

           setTimeout(()=>{
             wx.navigateBack()
           },1000)
         }else{
           wx.showToast({
             title: res.msg,
             duration: 1000,
             icon:'none'
           })
         }
      })
    }else{
      wx.navigateTo({
        url: '/pages/auth/auth',
      })
    }
  },
  bindphobe(){
    var that =this;
    if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(that.data.phone))) {
      wx.showToast({
        title: '请正确填写手机号',
        duration: 1000,
        icon: "none"
      })
      return;
    } 
    that.setData({ ishi: false })
    that._common()
    var time_ed = setInterval(()=>{
      var time = that.data.time;
        if(time == 0){
          clearInterval(time_ed)
          that.setData({time:60,ishi:true})
        }else{
          time--;
          that.setData({time})
        }
    },1000)
  },
  bindOfcose(){
    if (this.data.phone == "") {
      wx.showToast({
        title: '请正确填写手机号',
        duration: 1000,
        icon: "none"
      })
      return;
    }
    this._perfect()
  }  
})